package cn.edu.scau.daoImpl;

import java.util.List;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import cn.edu.scau.dao.PeopleDao;
import cn.edu.scau.entity.People;

@Repository("peopleDaoImpl")
public class PeopleDaoImpl extends BaseDaoImpl<People> implements PeopleDao {

	@Override
	public void updateAllPeopleColumnID(Integer columnID, Integer oldColumnID) {
		// 编写hql语句
		String hqlColumn = "update People p set ColumnID = :newColnmnID where ColumnID = :oldColnmnID";
		// 获得session
		Session session = getSessionFactory().openSession();
		// 执行操作
		session.createQuery(hqlColumn).
				// 设置参数
				setInteger("oldColnmnID", oldColumnID).setInteger("newColnmnID", columnID).executeUpdate();
		// flush到数据库
		session.flush();
		// 关闭session
		session.close();
	}

	/**
	 * 根据第几届来获取人物实体
	 */
	@Override
	public Long findCountByColumnID(Integer columnID) {

		Object[] objs = new Object[1];
		objs[0] = columnID;
		List<?> list = findByHql("select count(*) from People where ColumnID = ?0", objs);
		if (list != null && list.size() > 0) {
			return (Long) list.get(0);
		}
		return (long) 0;
	}

	/**
	 * 根据栏目ID（第几届），还有部门来获取人物实体
	 */
	@Override
	public List<People> findPeopleByColumnIDAndDepartment(Integer columnId, String department) {

		String hql = "from People as p where p.columnId= ?0 and p.department=?1";
		Object[] objs = new Object[2];
		objs[0] = columnId;
		objs[1] = department;
		return this.findByHql(hql, objs);
	}

	/**
	 * 根据栏目id获取实体属性
	 * 
	 * @param columnId
	 * @return
	 */
	public List getPropertyofPeopleManagement(Integer columnId) {

		String hql = "select p.people_id , p.name ,"
				+ " p.department , p.position , p.birthplace ,"
				+ " p.profession from People p where p.columnId=?0";
		
		Object[] objs = new Object[1];
		objs[0] = columnId;
		return this.findByHql(hql, objs);
	}

}
